<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['basic']=$this->plugin_setting('basic');
$return['banner']=$return['basic']['hdps'];
if($_RQ['op']=='main'){
	/*******main*******/
	//门票
	$id=$_RQ['ticketid'];
	
	if(!$id){
		exi('该门票可能已下架或已删除','error');
	}
	$suitid=$_RQ['suitid'];
	$ticket=pdo_get('jy_tour_spot_ticket',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$spot=pdo_get('jy_tour_spot',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$merch=pdo_get('jy_tour_merch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ticket['merchid']));
	$suitlist=json_decode($ticket['types'],true);
	$ticketprice=$suitlist[$suitid]['price'];
	$ticket['merch']=$merch;
	//获取最近几天的报价
	$begintime=time();
	$endtime=time()+2*24*3600;
	$data=timeList($begintime,$endtime);
	$return['ticket']=$ticket;
	$return['suitid']=$suitid;
	$return['data']=$data;
	$return['suitlist']=$suitlist;
	$return['price']=$ticketprice;
	//print_r($data);exit;
	$return['info']['ticket']=$ticket;
	$return['spot']=$spot;
	$return['post']=array('num'=>1,'suitid'=>$suitid);
	$return['post']['spot']=$spot;
	$return['post']['ticket']=$ticket;
	$return['nowyue']=date('Ym',TIMESTAMP);
	exi($return);
}
//提交
if($_RQ['op']=='submit'){
	$post=get_postdata();
	$way=$post['way'];
	//print_r($post);exit;
	if(!$post){
		exi('请完善相关信息！','error');
	}
	if(!$post['time']){
		exi('请选择出发时间！','error');
	}
	if(!$post['name']){
		exi('请选择联系人姓名！','error');
	}
	if(!$post['mobile']){
		exi('请填写联系电话！','error');
	}
	//生成订单
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'merchid'=>$way['merchid'],
		'ordersn'=>get_ordersn('jy_tour_order','ordersn'),
		'way_id'=>$way['id'],
		'go_time'=>$post['time'],
		'buynum'=>$post['buynum']?$post['buynum']:1,
		'name'=>$post['name'],
		'mobile'=>$post['mobile'],
		'price'=>($post['buynum']?$post['buynum']:1)*$way['price'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_tour_order',$insert);
	$id=pdo_insertid();
	$params['fee']=$insert['price'];
	$params['title']='旅游订单';
	$params['id']=$id;
	$params['plugin']='jy_tour';
	$params['back_url']='jy_tour/way.book/payresult';
	$return['payid']=cfc('pay')->create_pay_order($params);
	exi($return);
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_tour_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_tour_order',array('status'=>'1','fee'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_tour/order.list//status=1&id='.$order['id']);
}
if($_RQ['op']=='price'){
	$spot_id=$_RQ['spot_id'];
	$suitid=$_RQ['suitid'];
	$ticket_id=$_RQ['ticket_id'];
	//print_r($suitid.'ggg');exit;
	$startdate=date('Ymd');
	$day = $startdate;
    $year = date('Y',$day);
    $month = date('m',$day);
	//查询门票价格
	$ticket=pdo_get('jy_tour_spot_ticket',array('uniacid'=>$_SESSION['uniacid'],'id'=>$ticket_id));
	$suitlist=json_decode($ticket['types'],true);
	$ticketprice=$suitlist[$suitid]['price'];
	$ticketnum=$suitlist[$suitid]['num']?$suitlist[$suitid]['num']:0;
	$arr=array();
	if($return['basic']['no_today']==1){
		$no_today=1;
	}else{
		$no_today=0;
	}
	for($i=$no_today;$i<31;$i++){
		$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))));
		$daypirce=$weekid?$suitlist[$suitid]['price'.$weekid]:$ticketprice;
		$daypirce=$daypirce?:$ticketprice;
		$arr[]=array('day'=>date('Ymd', strtotime("+".$i." day")),'price'=>$daypirce,'number'=>$ticketnum,'weekid'=>$weekid);
	}
	foreach ($arr as $row)
    {
		 $price[$row['day']]['date'] = date('Y-m-d', strtotime($row['day']));
		 $price[$row['day']]['weekid'] =  $row['weekid'];
		 $price[$row['day']]['price'] =  $row['price'];
		 $price[$row['day']]['profit'] = 1;
		 $price[$row['day']]['suitid'] = $suitid;
         $price[$row['day']]['description'] = '描述';//描述
		 //已购买票数
		 $go_time=$price[$row['day']]['date'];
		 $isbuynums=pdo_getsum('jy_tour_order',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'ticket_id'=>$ticket_id,'go_time'=>$go_time,'spot_id'=>$spot_id,'suitid'=>$suitid),'buynum');
		 $price[$row['day']]['number'] = $row['number']-$isbuynums;//库存
	}
	$price_arr=$price; 
	exi($price_arr);
}
if($_RQ['op']=='next'){
	$post=get_postdata();
	if(empty($post['data']['usedate'])){
		exi('请选择使用时间！','error');
	}
	exi('','','jy_tour/spot.over_book//merchid='.$post['ticket']['merchid'].'&spot_id='.$post['spot']['id'].'&ticket_id='.$post['ticket']['id'].'&suitid='.$post['suitid'].'&go_time='.$post['data']['usedate'].'&buynum='.$post['num'].'&price='.$post['data']['price']);
	
}